var config = require("../../config.js");
// pages/annunciate/conntent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //博客ID
    blogId: '',
    reviewUserId: '',
    reviewUserName: '',
    type: 0,//评论类型 0 评论 1 回复
    deleteYes:null,

    //回复栏内容
    reviewtext: "请输入内容",


    //当前用户评论的内容
    comment: "",

    blog: {},

    "image": "../../image/img/img1.jpg",
    "name": "小王",
    "text": "四年级学生报名开始时间为今日（4月27日）15点，二、三年级报名开始时间为今日17点。",
    "time": "2023-12-30",
    commentList: [{
      "stu_num": "4203113324",
      "image": "../../image/img/img1.jpg",
      "username": "小王",
      "comment": "大四学生即将毕业，报考大学四六级只有最后一次机会. ",
      "time": "2023-12-30",
    }, {
      "stu_num": "4203113324",
      "image": "../../image/img/img1.jpg",
      "username": "小王",
      "comment": "大四学生即将毕业，报考大学四六级只有最后一次机会。 ",
      "time": "2023-12-30",
    }, {
      "stu_num": "4203113324",
      "image": "../../image/img/img1.jpg",
      "username": "小王",
      "comment": "大四学生即将毕业，报考大学四六级只有最后一次机会。 ",
      "time": "2023-12-30",
    }, {
      "stu_num": "4203113324",
      "image": "../../image/img/img1.jpg",
      "username": "小王",
      "type": 0,
      "targetUserName": "",
      "comment": "四年级学生报名开始时间为今日（4月27日）15点，二、三年级报名开始时间为今日17点。@全体成员 ",
      "time": "2023-12-30",
    }, {
      "stu_num": "4203113324",
      "image": "../../image/img/img1.jpg",
      "username": "小王",
      "type": 1,
      "targetUserName": "张三",
      "comment": "@全体成员 ",
      "time": "2023-12-30",
    }]
  },


  /**
   * 点击名字回复评论
   */
  review(e) {
    console.log(e);
    var reviewUserId = e.currentTarget.dataset.reviewuserid;
    if (reviewUserId != config.userId) {
      var reviewUserName = e.currentTarget.dataset.reviewusername;
      console.log("reviewUserId:" + reviewUserId);
      this.setData({
        reviewUserId: e.currentTarget.dataset.reviewuserid,
        reviewUserName: e.currentTarget.dataset.reviewusername,
        reviewtext: "回复" + reviewUserName,
        type: 1,
      })
    }

  },

  /**
   * 获取blog内容
   */
  getBlog(blogId) {
    wx.request({
      url: `http://localhost:8080/blog/${config.userId}/detail?blogId=${blogId}`,
      method: "GET",
      success: (res) => {
        console.log(res);
        this.setData({
          blog: res.data.data,
          deleteYes:res.data.data.createdBy === config.userId
        })
      },
      fail: (error) => {
        console.log(error);
      }

    })
  },

// 用户点击喜欢按钮，按钮颜色变化，like+1
  // 再次点击，按钮颜色变为灰色，
  // 从点赞数据表中寻找对应的用户，以及相应的博客，看一看是否已经点赞
  // 同理：关注也是如此
  likeadd() {
     // console.log(e.target.dataset.blog.blogId);
    var num = this.data.blog.likes;
    console.log("点赞数：" + num)
    //判断是否已经点赞
    // isshow = 1 : 已经点赞
    // isshow = 0 : 没有点赞
    var isshow = this.data.blog.liked;
    // const blogId = this.data.message[id].blogId
    console.log("是否已经点赞：" + isshow)
    // 1、没有点赞
    if (isshow == 0) {
      console.log("没有点赞")
      wx.request({
        url: `http://localhost:8080/liked/${config.userId}/like?blogId=${this.data.blog.id}`,
        method: 'GET',
          success: (res) => {
          this.setData({
            //得到新的数据
            // e.likes:20,
            [`blog.likes`]: ++num,
            [`blog.liked`]: 1,
            // likes:res.data
          })
          console.log(res.data)
        }
      })
    }

    //2.已经点赞
    else {
      console.log("已经点赞！")
      wx.request({
        url: `http://localhost:8080/liked/${config.userId}/likeDel?blogId=${this.data.blog.id}`,
        method: 'GET',
        success: (res) => {
          // e.target.dataset.blog.likes = 20;
          this.setData({
            [`blog.likes`]: --num,
            [`blog.liked`]: 0,
            page:this.page+1
          })
          console.log(res.data)
          // e.target.dataset.blog.likes = 20;
        }
      })
    }
    //  this.setData({
    //   [`message[${id}].likes`]: num, //es6模板语法（反撇号字符）
    //   // [`message[${i}].favor_img`]: "../../image/favor.png",
    //  })

  },

  /**
   * 删除id
   */
  delete(){
    console.log("删除");
    wx.request({
      url: `http://localhost:8080/blog/${config.userId}/deleted?blogId=${this.data.blogId}`,
      method: "GET",
      success(res) {
        if(res.data.code == 1){
          //跳转到主界面
          wx.switchTab({
            url: '/pages/home/home', // 此处替换为你想要跳转的页面的路径
            success: function(res) {
              // 跳转成功后的操作
            },
            fail: function(err) {
              // 跳转失败后的操作
            }
          });
        }
        else{
          //提示错误
        }
      },
      fail() {
      }
    })
  },

  /**
   * 获取评论列表
   */
  getCommentList(blogId) {
    console.log("blogId:" + blogId);
    var that = this;
    wx.request({
      url: `http://localhost:8080/comments/4203113324/commentList?blogId=${this.data.blogId}`,
      method: "GET",
      success(res) {
        console.log("获取评论列表成功");
        console.log(res);
        that.setData({
          commentList: res.data.data,
        })
      },
      fail() {
      }
    })
  },

  /**
   * 评论输入框内容改变，绑定内容
   */
  commentInput(e) {
    // console.log(  e.detail.value);
    this.setData({
      comment: e.detail.value
    });
  },

  /**
   * 提交评论内容
   */

  submit() {
    console.log(this.data.comment);
    wx.request({
      url: `http://localhost:8080/comments/${config.userId}/review`,
      method: "POST",
      data: {
        blogId: this.data.blogId,//博客ID
        comment: this.data.comment,//评论内容
        type: this.data.type,//评论类型
        targetUserId: this.data.reviewUserId,//回复目标用户ID
      },
      success: (res) => {
        console.log(res);
        if(res.data.code == 1){
          this.getCommentList(this.data.blogId)
          this.setData({
            comment:''
          })
          console.log("comment:",this.data.comment);
        }else{
          wx.showToast({
            icon:'none',
            title: res.data.msg,
          })
        }
      
      },
      fail: (error) => {
        console.log(error);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      blogId: options.blogId,
    })
    this.getBlog(options.blogId);
    this.getCommentList(options.blogId);
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})